package com.nowcoder.community.config;

import com.nowcoder.community.controller.interceptor.AlphaInterceptor;
import com.nowcoder.community.controller.interceptor.LoginRequiredInterceptor;
import com.nowcoder.community.controller.interceptor.LoginTicketInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//1.定义WebMvcConfig类实现WebMvcConfigurer接口
// 标明为配置类
public class WebMvcConfig implements WebMvcConfigurer {

    //2.注入拦截器类

    @Autowired
    private LoginRequiredInterceptor loginRequiredInterceptor;

    @Autowired
    private AlphaInterceptor alphaInterceptor;

    @Autowired
    private LoginTicketInterceptor loginTicketInterceptor;

    //3.注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //通过registry进行注册
       registry.addInterceptor(alphaInterceptor)
               //排除拦截路径
               .excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jepg")
               //指定拦截路径，若不指定则默认拦截所有，除了排除的
               .addPathPatterns("/register","/login");

        registry.addInterceptor(loginTicketInterceptor)
                //排除拦截路径
                .excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jepg");

        registry.addInterceptor(loginRequiredInterceptor)
                //排除拦截路径
                .excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jepg");
    }
}
